
package com.compuvest.webstat

import javax.servlet.ServletConfig
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/webstat/httpservice")
class HTTPService extends HttpServlet {

    def mysql

    def void init(ServletConfig config) {
	super.init(config)

        mysql = new MySQLService()
    }

    def void processRequest(request, response) {

        def json
        switch(request.getParameter('table')) {

            case 'epy':

                switch(request.getParameter('ex')) {
                    case 'getUsers'     : json = mysql.getEpyUsers(); break

                    case 'getGroups'    : json = mysql.getEpyGroups(request.getParameter('userid')); break
                    case 'editGroup'    : json = mysql.editEpyGroup(request.getParameter('userid'), request.getParameter('groupid')); break
                    case 'createGroup'  : json = mysql.createEpyGroup(request.getParameter('userid'), request.getParameter('cb')); break
                    case 'updateGroup'  : json = mysql.updateEpyGroup(request.getParameter('userid'), request.getParameter('groupid'), request.getParameter('cb')); break
                    case 'deleteGroup'  : json = mysql.deleteEpyGroup(request.getParameter('groupid')); break

                    case 'getItems'     : json = mysql.getEpyItems(request.getParameter('groupid')); break
                    case 'getItem'      : json = mysql.getEpyItem(request.getParameter('itemid')); break
                    case 'searchItem'   : json = mysql.searchEpyItem(request.getParameter('sku')); break
                    case 'insertItem'   : json = mysql.insertEpyItem(request.getParameter('itemid'), request.getParameter('groupid'), request.getParameter('sku')); break
                    case 'updateItem'   : json = mysql.updateEpyItem(request.getParameter('itemid'), request.getParameter('cb')); break
                    case 'deleteItem'   : json = mysql.deleteEpyItem(request.getParameter('itemid')); break

                    case 'getTestItem'  : json = mysql.getEpyTestItem(request.getParameter('sku')); break

                    default: json = '{}'
                }
                break

            case 'util':
                switch(request.getParameter('ex')) {
                    case 'getStatus'    : json = mysql.getGoogleItemsStatus(); break
                    default: json = '{}'
                }
                break
            default: json = '{}'
        }

        response.setContentType("text/html;charset=UTF-8")
        def out = response.getWriter()
        try {
            out.print(json)
        }
        finally {
            out.close()
        }
    }


    protected def getString(value) {
        return getString(value, "")
    }

    protected def getString(value, defValue) {
        return value ? value : defValue
    }

    protected def getInt(value) {
        return getInt(value, 0)
    }

    protected def getInt(value, defValue) {
        return value ? Integer.parseInt(value) : defValue
    }

    protected def getDouble(value) {
        return getDouble(value, 0d)
    }

    protected def getDouble(value, defValue) {
        return value ? Double.parseDouble(value) : defValue
    }


    def void doGet(HttpServletRequest request, HttpServletResponse response) {
        processRequest(request, response)
    }
    def void doPost(HttpServletRequest request, HttpServletResponse response) {
        processRequest(request, response)
    }
    String getServletInfo() {
        return "HTTPService"
    }
    def void destroy() {
        mysql = null
    }
}

